package com.zut.filter;

import com.alibaba.fastjson.JSON;
import com.zut.pojo.R;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@WebFilter(urlPatterns = "/*")
public class LoginCheckFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;

        String url = req.getRequestURL().toString();
        log.info("请求的url，{}",url);




        if (url.contains("login") || url.contains("backend") || url.contains("front") || url.contains("sendMsg")){

            log.info("登录或退出操作，放行");
            chain.doFilter(request, response);
            return;
        }

        /*
        *
        *员工登录判断 （pc端）
        * */

        if (req.getSession().getAttribute("employee") != null) {
            log.info("已登录，id为：{}",req.getSession().getAttribute("employee"));
            chain.doFilter(request, response);
            return;
        }


        /*
         *
         *用户登录判断（移动端)
         * */

        if (req.getSession().getAttribute("user") != null) {
            log.info("已登录，id为：{}",req.getSession().getAttribute("user"));
            chain.doFilter(request, response);
            return;
        }



        log.info("未登录");
        resp.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        return;

    }
}
